﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using System.Net.Mail;//引入空间

public partial class email邮件 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Email.Send("susu@163.com", "li3sun@qq.com", "没有主量", true, "<html>ooo啊啊，这不是LJ邮件啊</html>", "smtp.163.com", "用户名", "密码"))
        {
            Response.Write("send message");
        }
    }
}

public class Email {
    /// <summary> 
    /// 发送邮件 
    /// </summary> 
    /// <param name="from">发送人邮件地址</param> 
    /// <param name="to">接收人邮件地址</param> 
    /// <param name="subject">邮件主题</param> 
    /// <param name="isBodyHtml">是否是Html</param> 
    /// <param name="body">邮件体</param> 
    /// <param name="smtpHost">SMTP服务器地址</param> 
    /// <param name="userName">用户名</param> 
    /// <param name="password">密码</param> 
    /// <returns>是否成功</returns> 
    public static bool Send(string from, string to, string subject, bool isBodyHtml, string body, string smtpHost, string userName, string password)
    {
        string[] ts = to.Split(',');
        bool isSuccess = true;
        foreach (string t in ts)
        {
            try
            {
                MailMessage mm = new MailMessage();
                mm.From = new MailAddress(from);

                mm.To.Add(new MailAddress(t.Trim()));

                mm.Subject = subject;
                mm.IsBodyHtml = isBodyHtml;
                mm.Body = body;

                SmtpClient sc = new SmtpClient();
                sc.Host = smtpHost;

                sc.UseDefaultCredentials = true;//winform中不受影响，asp.net中，false表示不发送身份严正信息 
                //smtpClient.EnableSsl = true;//如果服务器不支持ssl则报，服务器不支持安全连接 错误 
                sc.Credentials = new System.Net.NetworkCredential(userName, password);
                sc.DeliveryMethod = SmtpDeliveryMethod.Network;

                sc.Send(mm);
            }
            catch
            {
                isSuccess = false;
            }
        }
        return isSuccess;
    }




}

